package com.itheima.reggie.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.UUID;


@RestController
@RequestMapping("/ailiPay")
public class aliPayController {
    private final String APP_ID = "2021000120616428";//应用的APPID
    //生成的应用私钥
    private final String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCWBUnCY0w9bdrs6sBx6CA502IJY/Uq3t2eJbDIRHR+NeRnOtZqsUbXGT9xj0qcanL6ofMyQMFtHQF8wq371jH7iMJzRW0YH1PhNvzwGZc3B2PPmNnCLGseemOSxkg31IMuyiv9pkRDl2XLsy6oK4bT9qV8GLF5mXHYDVdBthH23YYoGBENAplqhXak0k2azlnx74KWkl2h6eaWmkkSrdZwfSA9X0iYr4WlJjq4XjIvZEAoZpSnD4MZAb3ti0GaYJxgOf2o/NjUp/DAzBR9LYntqh2LZng1ju92etlQ6QqfDzW7zcfR8j6PhVeaL3dpFMtouC9qUNWahqXROU4JAICvAgMBAAECggEBAIhDX16wIMS/LpX5GDmeNP8xECAlab+51KRb6bnN693jh4V4PN+GDCk+JpE4LPXr/mnGhgZEKSy5pusyTSOsx5OIAc7/5Xp+ogqzjoVrSR1TmgYq+MfU3qs9UYo0jc4gOdNbESAAUIrwTib6pstbVcjbhC42o6UmifBLFdF61dBavAN+RCMF+VoWTgq0yVPTeao7HQh7de+5SmaFfb7No5wTdOar8dOBd2doXO017sWcbM4Buowp3Jl7Y/0OTNFHec8OGsJsmMNgydbgAVcH3//COTG4OGsFHcui8wz9/z91E3HVaWEi9NMkc1x89zd3ICu8pSshRavNrh3MwGjW19ECgYEAxjRlZCq+RO0QTU/8oALsl9RtT9+wYH1mgP87qNr//ayC07kJ8bTt94wyKuXFTCXi9vTeEkhquAOQRkOcapO9eHYAmvFLoTUiW+oUtpcXvhafGWGv5GUSQUrdOMkzpr517Qcmea8qxLXWa6uDX65HSRmusY5NyaJMfyOFzVaDpfcCgYEAwcQL40+EooTzYwydrz34gHoYtDk1m3i69+A/X1nKICFixLegfV6F7q87xJ/ttc7mexlU9dLAxzLpKKtE6phnX/ly1EEuD9+B8kzXN0Q/3lwecBYCsDOQyDG18rkMfRFwXmpT0T2b6DYLoZEvX4aeQYRW0PgJbOQowFcRGFmW7QkCgYAsx/PMgNfCTOls/lBuepqn5yfXbwuzkUGSjZgWG3zQ/KQGX/0+LgnB71v6UzWTcc/oygROjywFbVCwf2AcuEqLp+WVHEVhI9ciey2430RO/19s5aRO7wMD231F8jeISens5rurrIb2MLtEUlj5Si/y9SmCFku+7bto0kbN0OknmwKBgHFJmx26xzRyAQ8fOC5WUC1ltX3hmPseWkpEvSiVibzBaErVharnEe1cXNh4HPZYUp8UT30uTHHqzTFu+Wsj0gTmBoIc3b6AEizFSmtQpvrt5wnnhIjajULO9a9FDA0vngBFsF285KOevawoA4eZYHuz0UJ6UqBswXq7LQ6MitZpAoGAPWjBFg2+rZ09zOP4qhonXFbjRfHcoRXmzgo6jdLktfjK8pgR0JBJpgsjDkfukcrKqq+6Bvrri6vUjTV83uENsLmbqsVEOU/cIelwXyxLFGHS/TeemrYTHqpLzKOOhnJlKjdwbajp4b/clYkpJkhEYxN4+RZKHlxws8ufJgeV7F8=";
    private final String CHARSET = "UTF-8";
    //支付宝公钥
    private final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgNtIZQHPAzol+sIBxQy0VzAcm2lv494nIlLgivzKYeGlC+ylI22XlvX2TfFzLpc/9/w0b9jqh3pnIrlQw3Zs6HOEsY4UXFpK9Bn/gHWh6C1DOBhalCKiNDjXaJEqXQdCpQ3Xq851jmIMtScZltmXkcK/QBoeMPHoJN8xYAqRXDGt7SrCgSerzQXeKYwnZcdoy55uXBJbNjOI28ML+pQDqEUMwPQyM04kSjPXNe6jzWHbv9JvXu1Bo6D/yDBa0DzqHv5tYb6XvbwfmLhyAKvx9tdp+5hnA/Fee+nqONWiIGtZeBi59SWzMEncz671ls1ItHLAmsTh6clp7hjcgKz86wIDAQAB";
    //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL = "https://openapi.alipaydev.com/gateway.do";
    private final String FORMAT = "JSON";
    //签名方式
    private final String SIGN_TYPE = "RSA2";
    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
    private final String NOTIFY_URL = "http://127.0.0.1/notifyUrl";
    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
    private final String RETURN_URL = "http://127.0.0.1/returnUrl";


    @RequestMapping
    public void alipay(HttpServletResponse httpResponse) throws IOException {

        SecureRandom r = new SecureRandom();
        //实例化客户端,填入所需参数
        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //在公共参数中设置回跳和通知地址
        request.setReturnUrl(RETURN_URL);
        request.setNotifyUrl(NOTIFY_URL);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        //生成随机Id
        String out_trade_no = UUID.randomUUID().toString();
        //付款金额，必填
        String total_amount = Integer.toString(r.nextInt(9999999) + 1000000);
        //订单名称，必填
        String subject = "奥迪A8 2021款 A8L 60 TFSl quattro豪华型";
        //商品描述，可空
        String body = "尊敬的会员欢迎购买奥迪A8 2021款 A8L 60 TFSl quattro豪华型";
        request.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String form = "";
        try {
            form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + CHARSET);
        httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }


}
